<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>fastdfs集群版搭建 | Eetal&#39;s Blog</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="欢迎查看Eetal的第四篇博客–快速图片服务器fastdfs集群版的搭建笔者之前电脑空间拮据搭过一次，只能使用一台电脑不同端口来当服务器搭建的单机版fastdfs加了1T的硬盘，刚好有空今天顺便来把真·集群版的也完成一遍 FastDFS介绍参考：http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统它对文件进行管理，功能包括：文件存储、文件同步">
<meta property="og:type" content="article">
<meta property="og:title" content="fastdfs集群版搭建">
<meta property="og:url" content="http://eetal.gitee.io/blog/2018/06/08/fastdfs集群/index.html">
<meta property="og:site_name" content="Eetal&#39;s Blog">
<meta property="og:description" content="欢迎查看Eetal的第四篇博客–快速图片服务器fastdfs集群版的搭建笔者之前电脑空间拮据搭过一次，只能使用一台电脑不同端口来当服务器搭建的单机版fastdfs加了1T的硬盘，刚好有空今天顺便来把真·集群版的也完成一遍 FastDFS介绍参考：http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统它对文件进行管理，功能包括：文件存储、文件同步">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/fastdfs.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/vmx.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/ipconfig.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/fastdfsTar.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/nginxTar.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/perlTar.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/2018/06/08/fastdfs集群/images/fastdfs/shareFolder.jpg">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/fastdfs/nginx.jpg">
<meta property="og:updated_time" content="2018-06-08T14:32:48.223Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="fastdfs集群版搭建">
<meta name="twitter:description" content="欢迎查看Eetal的第四篇博客–快速图片服务器fastdfs集群版的搭建笔者之前电脑空间拮据搭过一次，只能使用一台电脑不同端口来当服务器搭建的单机版fastdfs加了1T的硬盘，刚好有空今天顺便来把真·集群版的也完成一遍 FastDFS介绍参考：http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统它对文件进行管理，功能包括：文件存储、文件同步">
<meta name="twitter:image" content="http://eetal.gitee.io/blog/images/fastdfs/fastdfs.jpg">
  
    <link rel="alternative" href="/atom.xml" title="Eetal&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="http://www.yangyitao.top/FiveChess/resource/ico/favicon.ico">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">Eetal</a></h1>
		</hgroup>

		
		<p class="header-subtitle">java coder</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/blog/">Home</a></li>
				        
							<li><a href="/blog/archives">Archives</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">Eetal</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">Eetal</h1>
			</hgroup>
			
			<p class="header-subtitle">java coder</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/blog/">Home</a></li>
		        
					<li><a href="/blog/archives">Archives</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-fastdfs集群" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/blog/2018/06/08/fastdfs集群/" class="article-date">
  	<time datetime="2018-06-07T16:11:39.497Z" itemprop="datePublished">2018-06-08</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      fastdfs集群版搭建
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>欢迎查看Eetal的第四篇博客–快速图片服务器fastdfs集群版的搭建<br>笔者之前电脑空间拮据<br>搭过一次，只能使用一台电脑不同端口来当服务器<br>搭建的单机版fastdfs<br>加了1T的硬盘，刚好有空<br>今天顺便来把真·集群版的也完成一遍</p>
<h2 id="FastDFS介绍"><a href="#FastDFS介绍" class="headerlink" title="FastDFS介绍"></a>FastDFS介绍</h2><p>参考：<a href="http://www.oschina.net/p/fastdfs" target="_blank" rel="noopener">http://www.oschina.net/p/fastdfs</a></p>
<p>FastDFS 是一个开源的分布式文件系统<br>它对文件进行管理，功能包括：<br>文件存储、文件同步、文件访问（文件上传、文件下载）等<br>解决了大容量存储和负载均衡的问题。<br>特别适合以文件为载体的在线服务，如相册网站、视频网站等等<br>FastDFS 服务端有两个角色：跟踪器（tracker）和存储器（storage）<br>跟踪器主要做调度工作，在访问上起负载均衡的作用<br>存储节点存储文件，完成文件管理的所有功能：存储、同步和提供存取接口<br><img src="/blog/images/fastdfs/fastdfs.jpg" alt="fastdfs架构"></p>
<h2 id="创建虚拟机"><a href="#创建虚拟机" class="headerlink" title="创建虚拟机"></a>创建虚拟机</h2><p>本次的搭建环境为，在虚拟机下的搭建<br>使用的镜像为centos6.9<br>首先创建6台虚拟机<br>2个跟踪者集群，两个group，每个group两个存储者集群<br>规定好每台虚拟机ip，后面配置静态ip<br><img src="/blog/images/fastdfs/vmx.jpg" alt="创建虚拟机"></p>
<h2 id="指定静态ip"><a href="#指定静态ip" class="headerlink" title="指定静态ip"></a>指定静态ip</h2><p>接下来，给每台虚拟机绑定静态ip<br>不然每次重启都会重新分配ip<br><img src="/blog/images/fastdfs/ipconfig.jpg" alt="配置ip"><br>使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ifconfig</span><br></pre></td></tr></table></figure></p>
<p>查看当前ip</p>
<h2 id="关闭防火墙"><a href="#关闭防火墙" class="headerlink" title="关闭防火墙"></a>关闭防火墙</h2><p>在实际生产环境中，应该只开放用到的端口<br>因为此处是在本地的虚拟机<br>所以为了方便，笔者直接关闭每台虚拟机防火墙<br>使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ service iptables stop</span><br></pre></td></tr></table></figure></p>
<p>此时，提示only use by root，需要root权限<br>于是我们先开启root权限<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ su root</span><br></pre></td></tr></table></figure></p>
<p>然后输入当前用户的密码，验证通过后即具备root权限<br>这时候使用刚刚的命令关闭防火墙就没问题了<br>接下来，禁止防火墙开机自启动<br>使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chkconfig iptables off</span><br></pre></td></tr></table></figure></p>
<p>至此，虚拟机环境的准备工作就完成了<br>接下来就是搭建fastdfs了</p>
<h2 id="下载工具包"><a href="#下载工具包" class="headerlink" title="下载工具包"></a>下载工具包</h2><p>后续用到的东西，笔者通过百度云分享在这里了<br>fastdfs相关<br><img src="/blog/images/fastdfs/fastdfsTar.jpg" alt="fastdfsTar"><br>链接：<a href="https://pan.baidu.com/s/1mNVVmdA8luxzHkPUostgqw" target="_blank" rel="noopener">https://pan.baidu.com/s/1mNVVmdA8luxzHkPUostgqw</a> 密码：nt0f<br>nginx相关<br><img src="/blog/images/fastdfs/nginxTar.jpg" alt="nginxTar"><br>链接：<a href="https://pan.baidu.com/s/1vVEarFoFJvtoDe1xZROH_A" target="_blank" rel="noopener">https://pan.baidu.com/s/1vVEarFoFJvtoDe1xZROH_A</a> 密码：05wp<br>perl相关<br><img src="/blog/images/fastdfs/perlTar.jpg" alt="perlTar"><br>链接：<a href="https://pan.baidu.com/s/1iEeunTzU_voTjLSxx3ALyg" target="_blank" rel="noopener">https://pan.baidu.com/s/1iEeunTzU_voTjLSxx3ALyg</a> 密码：2koi</p>
<h2 id="规定好存储路径"><a href="#规定好存储路径" class="headerlink" title="规定好存储路径"></a>规定好存储路径</h2><p>在每台tracker里：<br>使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir /opt/fastdfs_tracker</span><br></pre></td></tr></table></figure></p>
<p>创建/opt/fastdfs_tracker目录，作为tracker的base_path<br>在每台storager里：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir /opt/fastdfs_storage_info</span><br><span class="line">$ mkdir /opt/fastdfs_storage_data</span><br></pre></td></tr></table></figure></p>
<p>创建/opt/fastdfs_storage_info目录，作为存储storage间同步文件的存储目录<br>创建/opt/fastdfs_storage_data目录，作为storage的存储目录</p>
<h2 id="安装libfastcommon"><a href="#安装libfastcommon" class="headerlink" title="安装libfastcommon"></a>安装libfastcommon</h2><h3 id="此步每台fastdfs虚拟机都需要"><a href="#此步每台fastdfs虚拟机都需要" class="headerlink" title="此步每台fastdfs虚拟机都需要"></a>此步每台fastdfs虚拟机都需要</h3><p>上传压缩包到虚拟机有很多种方式<br>笔者采用较方便的，直接挂载文件夹<br><img src="images/fastdfs/shareFolder.jpg" alt="挂载虚拟机文件夹到本机文件夹"><br>挂载后，虚拟机中的/mnt/hgfs/下，就会具备挂载到的本机目录<br>如果没有hgfs目录，手动创建一下，重新启用共享就好了<br>进入挂载目录（此处以挂载到桌面文件夹为例）<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /mnt/hgfs/桌面/</span><br></pre></td></tr></table></figure></p>
<p>解压命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ tar zxf libfastcommonV1.0.7.tar.gz -C /</span><br></pre></td></tr></table></figure></p>
<p>-C / 参数代表指定解压到根目录，此处C必须大写<br>进入解压后的目录<br>执行make命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /libfastcommon-1.0.7/</span><br><span class="line">$ ./make.sh</span><br><span class="line">$ ./make.sh install</span><br></pre></td></tr></table></figure></p>
<p>根据安装信息，发现是在/usr/lib64,但fastDFS的主程序的lib目录是/usr/local/lib<br>所以要建立软连接<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ ln -s /usr/lib64/libfastcommon.so /usr/<span class="built_in">local</span>/lib/libfastcommon.so</span><br><span class="line">$ ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so</span><br><span class="line">$ ln -s /usr/lib64/libfdfsclient.so /usr/<span class="built_in">local</span>/lib/libfdfsclient.so</span><br><span class="line">$ ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so</span><br></pre></td></tr></table></figure></p>
<p>如果报缺乏gcc和perl环境的错误<br>先安装gcc和perl<br>使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ yum -y install gcc</span><br><span class="line">$ yum -y install gcc-c++</span><br></pre></td></tr></table></figure></p>
<p>安装gcc<br>接着安装perl<br>和刚刚一样，先解压perl的压缩包<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ tar zxf perl-5.20.2.tar.gz -C /</span><br><span class="line">$ makedir /usr/<span class="built_in">local</span>/perl</span><br><span class="line">$ ./Configure-des-Dprefix=/usr/<span class="built_in">local</span>/perl-Dusethreads=Uversiononly</span><br><span class="line">$ make</span><br><span class="line">$ make install</span><br><span class="line">$ perl -version</span><br></pre></td></tr></table></figure></p>
<p>然后就可以使用刚刚的两个命令安装libfastcommon，并创建软连接了</p>
<h2 id="安装fastdfs"><a href="#安装fastdfs" class="headerlink" title="安装fastdfs"></a>安装fastdfs</h2><h3 id="此步每台fastdfs虚拟机都需要-1"><a href="#此步每台fastdfs虚拟机都需要-1" class="headerlink" title="此步每台fastdfs虚拟机都需要"></a>此步每台fastdfs虚拟机都需要</h3><p>进入挂载目录（此处以挂载到桌面文件夹为例）<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /mnt/hgfs/桌面/</span><br></pre></td></tr></table></figure></p>
<p>解压命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ tar zxf FastDFS_v5.05.tar.gz -C /</span><br></pre></td></tr></table></figure></p>
<p>-C / 参数代表指定解压到根目录，此处C必须大写<br>进入解压后的目录<br>执行make命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /FastDFS/</span><br><span class="line">$ ./make.sh</span><br><span class="line">$ ./make.sh install</span><br></pre></td></tr></table></figure></p>
<p>至此，所有fastdfs服务机必备的步骤就完成，接下来就是分别对tracker和storage配置了</p>
<h2 id="配置tracker"><a href="#配置tracker" class="headerlink" title="配置tracker"></a>配置tracker</h2><p>以下操作对于两台tracker进行<br>复制并编辑配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /etc/fdfs</span><br><span class="line">$ cp tracker.conf.sample trakcer.conf</span><br><span class="line">$ vi trakcer.conf</span><br></pre></td></tr></table></figure></p>
<p>修改如下几个选项（#及后面内容不用输入进去）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">disabled=false  #启用配置文件</span><br><span class="line">port=22122  #使用端口</span><br><span class="line">base_path=/opt/fastdfs_tracker  #数据和日志文件存储目录</span><br></pre></td></tr></table></figure></p>
<p>按Esc<br>输入wq，回车保持并退出<br>启动tracker<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart</span><br></pre></td></tr></table></figure></p>
<p>修改用户自定义开机脚本，配置开机自动启动tracker<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /etc/rc.d/rc.local</span><br></pre></td></tr></table></figure></p>
<p>在文件末尾加上开启tracker的命令<br>查看端口<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ netstat -untlp|grep 22122</span><br></pre></td></tr></table></figure></p>
<p>有显示就是启动成功了<br>至此，完成了tracker的搭建<br>tracker的nginx负载均衡我们搭完storage再搭建<br>因为涉及到storage的缓存</p>
<h2 id="配置storage"><a href="#配置storage" class="headerlink" title="配置storage"></a>配置storage</h2><p>以下操作对于四台storage进行<br>不同的group对group_name进行修改<br>同一个group的storage的port必须使用一样的<br>复制并编辑配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /etc/fdfs</span><br><span class="line">$ cp storage.conf.sample storage.conf</span><br><span class="line">$ vi storage.conf</span><br></pre></td></tr></table></figure></p>
<p>修改如下几个选项（#及后面内容不用输入进去）<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">disabled=false	#启用配置文件</span><br><span class="line"></span><br><span class="line">group_name=group1	#组名（第一组为 group1，第二组为 group2）</span><br><span class="line"></span><br><span class="line">port=23001	#storage 的端口号,同一个组的 storage 端口号必须相同</span><br><span class="line"></span><br><span class="line">base_path=/opt/fastdfs_storage_info	#设置 storage 的日志目录</span><br><span class="line"></span><br><span class="line">store_path0=/opt/fastdfs_storage_data	#存储路径</span><br><span class="line"></span><br><span class="line">store_path_count=1	#存储路径个数，需要和 store_path 个数匹配</span><br><span class="line"></span><br><span class="line">tracker_server=192.168.156.151:22122	#tracker 服务器的 IP 地址和端口</span><br><span class="line"></span><br><span class="line">tracker_server=192.168.156.152:22122	#多个 tracker 直接添加多条配置</span><br></pre></td></tr></table></figure></p>
<p>按Esc<br>输入wq，回车保持并退出<br>启动storage<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart</span><br></pre></td></tr></table></figure></p>
<p>修改用户自定义开机脚本，配置开机自动启动tracker<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /etc/rc.d/rc.local</span><br></pre></td></tr></table></figure></p>
<p>在文件末尾加上开启storage的命令<br>查看端口<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ netstat -untlp|grep 23001</span><br></pre></td></tr></table></figure></p>
<p>有显示就是启动成功了<br>至此，完成了storage的搭建</p>
<h2 id="查看所有节点"><a href="#查看所有节点" class="headerlink" title="查看所有节点"></a>查看所有节点</h2><p>完成所有tracker和storage的启动后<br>可以在任意storage机器使用命令<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/bin/fdfs_monitor /etc/fdfs/storage.conf</span><br></pre></td></tr></table></figure></p>
<p>查看整个fastdfs系统的信息</p>
<h2 id="测试文件上传"><a href="#测试文件上传" class="headerlink" title="测试文件上传"></a>测试文件上传</h2><p>复制，修改任意tracker机器里的client.conf配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /etc/fdfs</span><br><span class="line">$ cp client.conf.sample client.conf</span><br><span class="line">$ vi client.conf</span><br></pre></td></tr></table></figure></p>
<p>改动base_path和配置tracker_server<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">base_path=/opt/fastdfs_storage_info</span><br><span class="line">tracker_server=192.168.156.151:22122</span><br><span class="line">tracker_server=192.168.156.152:22122</span><br></pre></td></tr></table></figure></p>
<p>上传命令(最后一个参数代表要上传的文件，此处为了方便使用client.conf上传测试)<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/client.conf</span><br></pre></td></tr></table></figure></p>
<p>如果有返回一个以group开头的字符串则是上传成功<br>至此，fastdfs的搭建我们已经完成近一半</p>
<h2 id="storage安装nginx模块"><a href="#storage安装nginx模块" class="headerlink" title="storage安装nginx模块"></a>storage安装nginx模块</h2><p>此步骤每台storage都要安装<br>storage安装nginx的目的是作为服务器提供http方式访问文件，并解决不同storage之间同步延迟的问题<br>先创建一个安装nginx的目录<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir /usr/<span class="built_in">local</span>/nginx</span><br><span class="line">$ <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx</span><br></pre></td></tr></table></figure></p>
<p>解压安装包修改配置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ tar zxf /mnt/hgfs/桌面/fastdfs-nginx-module_v1.16.tar.gz -C .</span><br><span class="line">$ tar zxf /mnt/hgfs/桌面/nginx-1.8.0.tar.gz -C .</span><br><span class="line">$ <span class="built_in">cd</span> fastdfs-nginx-module</span><br><span class="line">$ vi src/config</span><br></pre></td></tr></table></figure></p>
<p>修改CORE_INCS的值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CORE_INCS=&quot;$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/&quot;</span><br></pre></td></tr></table></figure></p>
<h3 id="安装编译nginx需要的依赖"><a href="#安装编译nginx需要的依赖" class="headerlink" title="安装编译nginx需要的依赖"></a>安装编译nginx需要的依赖</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel</span><br></pre></td></tr></table></figure>
<p>进入nginx目录并安装module<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx/nginx-1.8.0</span><br><span class="line">$ ./configure --prefix=/usr/<span class="built_in">local</span>/nginx --add-module=/usr/<span class="built_in">local</span>/nginx/fastdfs-nginx-module/src</span><br><span class="line">$ make &amp;&amp; make install</span><br></pre></td></tr></table></figure></p>
<h3 id="配置nginx"><a href="#配置nginx" class="headerlink" title="配置nginx"></a>配置nginx</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx</span><br><span class="line">$ vi conf/nginx.conf</span><br></pre></td></tr></table></figure>
<p>在listen 80那个server里面添加<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">location ~/group1/M00&#123;</span><br><span class="line">root /opt/fastdfs_storage_data/data;</span><br><span class="line">ngx_fastdfs_module;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>不同的group对应不同的路径<br>比如对于group2里的storage<br>location就是 ~/group2/M00</p>
<h3 id="复制nginx模块的配置文件并配置"><a href="#复制nginx模块的配置文件并配置" class="headerlink" title="复制nginx模块的配置文件并配置"></a>复制nginx模块的配置文件并配置</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ cp /usr/<span class="built_in">local</span>/nginx/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs</span><br><span class="line">$ vi /etc/fdfs/mod_fastdfs.conf</span><br></pre></td></tr></table></figure>
<p>修改对应内容，此处以group1为例<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">connect_timeout=10</span><br><span class="line"></span><br><span class="line">base_path=/opt/fastdfs_storage_info</span><br><span class="line"></span><br><span class="line">tracker_server=192.168.156.151:22122</span><br><span class="line"></span><br><span class="line">tracker_server=192.168.156.152:22122</span><br><span class="line"></span><br><span class="line">storage_server_port=23001</span><br><span class="line"></span><br><span class="line">group_name=group1</span><br><span class="line"></span><br><span class="line">url_have_group_name = true</span><br><span class="line"></span><br><span class="line">store_path0=/opt/fastdfs_storage_data</span><br><span class="line"></span><br><span class="line">group_count = 2</span><br><span class="line"></span><br><span class="line">[group1]</span><br><span class="line"></span><br><span class="line">group_name=group1</span><br><span class="line"></span><br><span class="line">storage_server_port=23001</span><br><span class="line"></span><br><span class="line">store_path_count=2</span><br><span class="line"></span><br><span class="line">store_path0=/opt/fastdfs_storage_data</span><br><span class="line"></span><br><span class="line">store_path1=/opt/fastdfs_storage_data</span><br><span class="line"></span><br><span class="line">[group2]</span><br><span class="line"></span><br><span class="line">group_name=group2</span><br><span class="line"></span><br><span class="line">storage_server_port=23002</span><br><span class="line"></span><br><span class="line">store_path_count=2</span><br><span class="line"></span><br><span class="line">store_path0=/opt/fastdfs_storage_data</span><br><span class="line"></span><br><span class="line">store_path1=/opt/fastdfs_storage_data</span><br></pre></td></tr></table></figure></p>
<p>复制fastdfs提供的配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /FastDFS/conf</span><br><span class="line">$ cp http.conf mime.types /etc/fdfs/</span><br></pre></td></tr></table></figure></p>
<p>建立软连接，<br>将storage文件存储目录链接到实际存放数据的目录<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ln -s /opt/fastdfs_storage_data/data /opt/fastdfs_storage_data/data/M00</span><br></pre></td></tr></table></figure></p>
<p>启动nginx<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/<span class="built_in">local</span>/nginx/sbin/nginx</span><br></pre></td></tr></table></figure></p>
<p>因为我们用的是80端口<br>在浏览器输入<a href="http://storage的ip地址" target="_blank" rel="noopener">http://storage的ip地址</a><br>能访问到nginx的界面则代表storage配合nginx成功了<br>将启动nginx命令写入开机文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /etc/rc.d/rc.local</span><br></pre></td></tr></table></figure></p>
<h2 id="tracker中安装nginx"><a href="#tracker中安装nginx" class="headerlink" title="tracker中安装nginx"></a>tracker中安装nginx</h2><p>tracker安装nginx主要目的是负载均衡，反向代理以及缓存服务<br>以下步骤对于所有tracker都是必须<br>创建缓存目录<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir -p /opt/cache/nginx/proxy_cache</span><br><span class="line">$ mkdir -p /opt/cache/nginx/proxy_cache/tmp</span><br></pre></td></tr></table></figure></p>
<p>创建安装nginx的目录<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir /usr/<span class="built_in">local</span>/nginx</span><br><span class="line">$ <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx</span><br></pre></td></tr></table></figure></p>
<p>解压安装包修改配置<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ tar zxf /mnt/hgfs/桌面/nginx-1.8.0.tar.gz -C .</span><br><span class="line">$ tar zxf /mnt/hgfs/桌面/ngx_cache_purge-2.3.tar.gz -C .</span><br></pre></td></tr></table></figure></p>
<h3 id="安装编译nginx需要的依赖-1"><a href="#安装编译nginx需要的依赖-1" class="headerlink" title="安装编译nginx需要的依赖"></a>安装编译nginx需要的依赖</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel</span><br></pre></td></tr></table></figure>
<p>安装nginx<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /usr/<span class="built_in">local</span>/nginx/nginx-1.8.0</span><br><span class="line">$ ./configure --prefix=/usr/<span class="built_in">local</span>/nginx --add-module=/usr/<span class="built_in">local</span>/nginx/ngx_cache_purge-2.3</span><br><span class="line">$ make &amp;&amp; make install</span><br></pre></td></tr></table></figure></p>
<p>###配置nginx<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</span><br></pre></td></tr></table></figure></p>
<p>修改内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections  1024;</span><br><span class="line">    use epoll;</span><br><span class="line">&#125;</span><br><span class="line">http&#123;</span><br><span class="line">	tcp_nopush	on;</span><br><span class="line">	</span><br><span class="line">	#gzip on;</span><br><span class="line"></span><br><span class="line">	#设置缓存</span><br><span class="line"></span><br><span class="line">	server_names_hash_bucket_size 128;</span><br><span class="line"></span><br><span class="line">	client_header_buffer_size 32k;</span><br><span class="line"></span><br><span class="line">	large_client_header_buffers 4 32k;</span><br><span class="line"></span><br><span class="line">	client_max_body_size 300m;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	proxy_redirect off;</span><br><span class="line"></span><br><span class="line">	proxy_set_header Host $http_host;</span><br><span class="line"></span><br><span class="line">	proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line"></span><br><span class="line">	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line"></span><br><span class="line">	proxy_connect_timeout 90;</span><br><span class="line"></span><br><span class="line">	proxy_send_timeout 90;</span><br><span class="line"></span><br><span class="line">	proxy_read_timeout 90;</span><br><span class="line"></span><br><span class="line">	proxy_buffer_size 16k;</span><br><span class="line"></span><br><span class="line">	proxy_buffers 4 64k;</span><br><span class="line"></span><br><span class="line">	proxy_busy_buffers_size 128k;</span><br><span class="line"></span><br><span class="line">	proxy_temp_file_write_size 128k;</span><br><span class="line"></span><br><span class="line">	#设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限 </span><br><span class="line">	proxy_cache_path /opt/cache/nginx/proxy_cache levels=1:2 </span><br><span class="line">	keys_zone=http-cache:200m max_size=1g inactive=30d;</span><br><span class="line">	proxy_temp_path /opt/cache/nginx/proxy_cache/tmp;</span><br><span class="line"></span><br><span class="line">	#设置 group1 的服务器</span><br><span class="line"></span><br><span class="line">	upstream fdfs_group1 &#123;</span><br><span class="line"></span><br><span class="line">	server 192.168.156.153 weight=1 max_fails=2 fail_timeout=30s; </span><br><span class="line">	server 192.168.156.154 weight=1 max_fails=2 fail_timeout=30s;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	#设置 group2 的服务器</span><br><span class="line"></span><br><span class="line">	upstream fdfs_group2 &#123;</span><br><span class="line"></span><br><span class="line">	server 192.168.156.155 weight=1 max_fails=2 fail_timeout=30s; </span><br><span class="line">	server 192.168.156.156 weight=1 max_fails=2 fail_timeout=30s;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">	server &#123;</span><br><span class="line"></span><br><span class="line">	listen	80;</span><br><span class="line"></span><br><span class="line">	server_name	localhost;</span><br><span class="line"></span><br><span class="line">	#设置 group 的负载均衡参数</span><br><span class="line"></span><br><span class="line">	location /group1/M00 &#123;</span><br><span class="line"></span><br><span class="line">	proxy_next_upstream http_502 http_504 error timeout invalid_header; </span><br><span class="line">	proxy_cache http-cache;</span><br><span class="line">	proxy_cache_valid 200 304 12h;</span><br><span class="line">	proxy_cache_key $uri$is_args$args;</span><br><span class="line">	proxy_pass http://fdfs_group1;</span><br><span class="line">	expires 30d;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	location /group2/M00 &#123;</span><br><span class="line"></span><br><span class="line">	proxy_next_upstream http_502 http_504 error timeout invalid_header; </span><br><span class="line">	proxy_cache http-cache;</span><br><span class="line">	proxy_cache_valid 200 304 12h;</span><br><span class="line">	proxy_cache_key $uri$is_args$args;</span><br><span class="line">	proxy_pass http://fdfs_group2;</span><br><span class="line">	expires 30d;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	#设置清除缓存的访问权限</span><br><span class="line"></span><br><span class="line">	location ~/purge(/.*) &#123;</span><br><span class="line">	allow 127.0.0.1;</span><br><span class="line">	allow 192.168.1.0/24;</span><br><span class="line">	deny all;</span><br><span class="line">	proxy_cache_purge http-cache $1$is_args$args;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>启动nginx<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/<span class="built_in">local</span>/nginx/sbin/nginx</span><br></pre></td></tr></table></figure></p>
<p>因为我们用的是80端口<br>在浏览器输入<a href="http://storage的ip地址" target="_blank" rel="noopener">http://storage的ip地址</a><br>能访问到nginx的界面则代表storage配合nginx成功了<br>将启动nginx命令写入开机文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /etc/rc.d/rc.local</span><br></pre></td></tr></table></figure></p>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><h3 id="机器测试"><a href="#机器测试" class="headerlink" title="机器测试"></a>机器测试</h3><p>首先，访问每台服务器的nginx首页<br><img src="/blog/images/fastdfs/nginx.jpg" alt="nginx1"><br>每台服务器的nginx都可以访问<br>OK!!!<br>如果有某一台访问不了<br>查看nginx目录下log目录里的日志文件<br>根据提示查错</p>
<h3 id="上传测试"><a href="#上传测试" class="headerlink" title="上传测试"></a>上传测试</h3><p>复制，修改任意tracker机器里的client.conf配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> /etc/fdfs</span><br><span class="line">$ cp client.conf.sample client.conf</span><br><span class="line">$ vi client.conf</span><br></pre></td></tr></table></figure></p>
<p>改动base_path和配置tracker_server<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">base_path=/opt/fastdfs_storage_info</span><br><span class="line">tracker_server=192.168.156.151:22122</span><br><span class="line">tracker_server=192.168.156.152:22122</span><br></pre></td></tr></table></figure></p>
<p>上传命令(最后一个参数代表要上传的文件，此处为了方便使用client.conf上传测试)<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /etc/fdfs/client.conf</span><br></pre></td></tr></table></figure></p>
<p>上传后得到一个返回的文件名<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">group2/M00/00/00/wKicm1saiXGALps_AAAF4Qywnmo39.conf</span><br></pre></td></tr></table></figure></p>
<p>这时候我们在浏览器访问任意一台tracker的ip加上这个文件名<br><a href="http://trackerip/group2/M00/00/00/wKicm1saiXGALps_AAAF4Qywnmo39.conf" target="_blank" rel="noopener">http://trackerip/group2/M00/00/00/wKicm1saiXGALps_AAAF4Qywnmo39.conf</a><br>都可以下载该文件<br>nginx会自动根据group选择不同的upstream，并根据负载均衡计算<br>跳转到某个storage服务器，看浏览器地址变化就明白了<br>至此，我们完成了采用Epoll进行负载均衡的fastdfs的搭建</p>
<h2 id="进阶内容–keepalived实现tracker高可用负载"><a href="#进阶内容–keepalived实现tracker高可用负载" class="headerlink" title="进阶内容–keepalived实现tracker高可用负载"></a>进阶内容–keepalived实现tracker高可用负载</h2><p>在每台tracker上<br>修改nginx的配置文件<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ vi /usr/<span class="built_in">local</span>/nginx/conf/nginx.conf</span><br></pre></td></tr></table></figure></p>
<p>修改内容<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections  1024;</span><br><span class="line">    #use epoll;</span><br><span class="line">&#125;</span><br><span class="line">http&#123;</span><br><span class="line">	#tcp_nopush	on;</span><br><span class="line">	</span><br><span class="line">	#gzip on;</span><br><span class="line"></span><br><span class="line">	keepalive_timeout	65;</span><br><span class="line"></span><br><span class="line">	# Tracker Proxy</span><br><span class="line"></span><br><span class="line">	upstream fastdfs_tracker &#123;</span><br><span class="line"></span><br><span class="line">	server 192.168.156.151 weight=1 max_fails=2 fail_timeout=30s; </span><br><span class="line">	server 192.168.156.152 weight=1 max_fails=2 fail_timeout=30s;</span><br><span class="line">	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	location /dfs &#123;		</span><br><span class="line">	root	html;		</span><br><span class="line">	index	index.html index.htm;</span><br><span class="line"></span><br><span class="line">	proxy_pass	http://fastdfs_tracker/;</span><br><span class="line"></span><br><span class="line">	proxy_set_header Host	$http_host;</span><br><span class="line"></span><br><span class="line">	proxy_set_header Cookie $http_cookie;</span><br><span class="line"></span><br><span class="line">	proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line"></span><br><span class="line">	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; </span><br><span class="line">	proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 300m;</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这个时候，下载文件的路径就不再是trackerip+返回的文件名了<br>而是通过这个trackerip/dfs/+返回的文件名<br>这时候由keepalived程序计算<br>判断访问哪边的tracker，实现高可用<br>keepalived就是通过定时互相发送心跳，定时检查主从服务机的健康状态<br>判断服务机是否还活着，当主服务器死了，就由从服务器接手<br>配置完毕，重启nginx<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/<span class="built_in">local</span>/nginx/sbin/nginx -s reload</span><br></pre></td></tr></table></figure></p>
<h3 id="更多精彩内容"><a href="#更多精彩内容" class="headerlink" title="更多精彩内容"></a>更多精彩内容</h3><h3 id="请移步"><a href="#请移步" class="headerlink" title="请移步"></a>请移步</h3><p>个人主页: <a href="http://yangyitao.top" target="_blank" rel="noopener">yangyitao.top</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2018/06/12/python网络爬虫并输出excel/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          Python网络爬虫并输出到excel
        
      </div>
    </a>
  
  
    <a href="/blog/2018/06/07/Docker01-mongodb/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">Docker安装mongodb</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="fastdfs集群" data-title="fastdfs集群版搭建" data-url="http://eetal.gitee.io/blog/blog/2018/06/08/fastdfs集群/"  data-images="/blog/images/head.jpg" data-content="fastdfs集群版搭建">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2019 Eetal
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/blog/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>